//
//  BwtAdEventManager.m
//  bwt_ad_flutter
//
//  Created by ciba on 2024/8/29.
//

#import "BwtAdEventManager.h"

@implementation BwtAdEventManager

/// 单例
+ (BwtAdEventManager *)getBwtAdEventManager {
    static BwtAdEventManager *bwtAdEventManager;
    if(!bwtAdEventManager) {
        bwtAdEventManager = [[BwtAdEventManager alloc] init];
    }
    return bwtAdEventManager;
}

- (void)setFlutterMethodChannel:(FlutterMethodChannel*)channel {
    _channel = channel;
}

- (void)postOnAdLoadEvent:(AdParams*)adParams {
    [self postOnAdEvent:adParams adEvent:@"OnAdLoad" message:@"" extras: nil];
}

//信息流自渲染的onload 需要把自渲染数据传出去
- (void)postNativeAdOnAdLoadEvent:(AdParams*)adParams {
    [self postNativeAdOnAdEvent:adParams adEvent:@"OnAdLoad" message:@"" extras: nil];
}


- (void)postOnAdLoadFailedEvent:(AdParams*)adParams message:(NSString*)message {
    [self postOnAdEvent:adParams adEvent:@"OnAdFailed" message:message extras: nil];
}

- (void)postOnAdMeasuredEvent:(AdParams*)adParams extras:(NSMutableDictionary*)extras {
    [self postOnAdEvent:adParams adEvent:@"OnAdMeasured" message:@"" extras: extras];
}

- (void)postOnAdExposeEvent:(AdParams*)adParams {
    [self postOnAdEvent:adParams adEvent:@"OnAdExpose" message:@"" extras: nil];
}

- (void)postOnAdClickEvent:(AdParams*)adParams {
    [self postOnAdEvent:adParams adEvent:@"OnAdClick" message:@"" extras: nil];
}

- (void)postOnAdCloseEvent:(AdParams*)adParams {
    [self postOnAdEvent:adParams adEvent:@"OnAdClose" message:@"" extras: nil];
}

- (void)postOnAdRenderEvent:(AdParams*)adParams {
    [self postOnAdEvent:adParams adEvent:@"OnAdRender" message:@"" extras: nil];
}

- (void)postOnAdRewardEvent:(AdParams*)adParams {
    [self postOnAdEvent:adParams adEvent:@"OnAdReward" message:@"" extras: nil];
}

- (void)postOnAdEvent:(AdParams*)adParams adEvent:(NSString*)adEvent message:(NSString*)message extras:(NSMutableDictionary*)extras {
    if (_channel) {
        NSMutableDictionary *arguments = [[NSMutableDictionary alloc] initWithCapacity:0];
        [arguments setObject:adParams.adLoaderId forKey:@"adLoaderId"];
        [arguments setObject:adParams.adId forKey:@"adId"];
        [arguments setObject:adEvent forKey:@"adEvent"];
        [arguments setObject:message forKey:@"adMsg"];
        if (extras) {
            [arguments setValuesForKeysWithDictionary:extras];
        }
        
        [_channel invokeMethod:@"onAdEvent" arguments:arguments];
    }
}
- (void)postNativeAdOnAdEvent:(AdParams*)adParams adEvent:(NSString*)adEvent message:(NSString*)message extras:(NSMutableDictionary*)extras {
    if (_channel) {
        NSMutableDictionary *arguments = [[NSMutableDictionary alloc] initWithCapacity:0];
        [arguments setObject:adParams.adLoaderId forKey:@"adLoaderId"];
        [arguments setObject:adParams.adId forKey:@"adId"];
        [arguments setObject:adEvent forKey:@"adEvent"];
        [arguments setObject:message forKey:@"adMsg"];
        [arguments setObject:adParams.nativeData forKey:@"nativeData"];
        [arguments setObject:@"loadNativeAd" forKey:@"method"];
        if (extras) {
            [arguments setValuesForKeysWithDictionary:extras];
        }
        
        [_channel invokeMethod:@"onAdEvent" arguments:arguments];
    }
}
@end
